﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ATM_Pro.Query
{
    using AForge.Imaging.Filters;
    using System.IO;
    using System.Diagnostics;

    public partial class ImageDetailsForm : Form
    {

        ImageDetail details;
        private Image preImg = null;

        public ImageDetailsForm()
        {
            InitializeComponent();
        }

        public ImageDetail Img
        {
            set
            {
                byte[] buff = File.ReadAllBytes(value.SavePath);
                this.piceImg.Image = Image.FromStream(new MemoryStream(buff));
                this.details = value;
                this.txtSavePath.Text = details.SavePath;
                this.captureTime.Text = value.CaptureTime.ToString();
                this.txtATMadd.Text = value.AtmAddress;
                string type = details.PackType.ToString();
                this.txtPicType.Text = type;
                if (type.Equals(PacketType.TradeMessage.ToString()))
                {
                    this.txtDetails.Text = "交易卡号： " + details.CardId.ToString();
                }
                else if (type.Equals(PacketType.AlarmMessage.ToString()))
                {
                    this.txtDetails.Text = string.Format("报警编号： {0} 报警级别： {1} 报警类型： {2}", details.WarnId.ToString(),
                                                            details.WarnLevel.ToString(),
                                                            details.WarnType.ToString());
                }
            }
        }

        private void ImageDetailsForm_Load(object sender, EventArgs e)
        {

        }

        private void tsbBrightness_Click(object sender, EventArgs e)
        {
            IPLab.BrightnessForm frm = new IPLab.BrightnessForm();
            frm.Image = (Bitmap)this.piceImg.Image;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.ApplyFilter(frm.Filter);
            }

            frm.Dispose();
        }

        private void ApplyFilter(IFilter filter)
        {
            try
            {
                // set wait cursor
                this.Cursor = Cursors.WaitCursor;

                // apply filter to the image
                Bitmap newImage = filter.Apply((Bitmap)this.piceImg.Image);

                if (preImg == null)
                {
                    preImg = this.piceImg.Image;
                }

                this.piceImg.Image = newImage;
                this.tspSave.Enabled = true;
                this.tspRestore.Enabled = true;


            }
            catch (ArgumentException)
            {
                MessageBox.Show("Selected filter can not be applied to the image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                // restore cursor
                this.Cursor = Cursors.Default;
            }
        }

        private void tspSharpen_Click(object sender, EventArgs e)
        {
            this.ApplyFilter(new Sharpen());
        }

        private void tspContrast_Click(object sender, EventArgs e)
        {
            IPLab.ContrastForm frm = new IPLab.ContrastForm();
            frm.Image = (Bitmap)this.piceImg.Image;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                this.ApplyFilter(frm.Filter);
            }

            frm.Dispose();
        }

        private void tspRestore_Click(object sender, EventArgs e)
        {
            if (preImg != null)
            {
                this.piceImg.Image = preImg;
            }
        }

        private void tspSave_Click(object sender, EventArgs e)
        {
            DialogResult result
                = MessageBox.Show(this,
                "确定要保存所做的修改?",
                "请确认",
                MessageBoxButtons.YesNo,
                MessageBoxIcon.Question);

            if (result != DialogResult.OK)
            {
                return;
            }

            this.piceImg.Image.Save(this.details.SavePath);

        }

        private void ImageDetailsForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.piceImg.Dispose();
        }

        private void tspDisplayVideo_Click(object sender, EventArgs e)
        {
            if (details.SavePath != null)
            {
                string temp = details.SavePath;
                string fileName = Path.GetFileName(temp);
                temp = temp.Replace(PacketType.TradeMessage.ToString() + "\\" + Properties.Settings.Default.BigImg, PacketType.RealtimeMessage.ToString());
                string videoPath = temp.Substring(0, temp.Length - 1 - fileName.Length) + ".avi";
              try
              {
                  if (File.Exists(videoPath))
                      Process.Start(videoPath);
                  else
                      MessageBox.Show("没有找到相关视频！");
      
              }
              catch (System.Exception ex)
              {
                   MessageBox.Show("没有找到默认的视频播放软件！");              	
              }
               
            }
        }
    }
}
